package com.plugin.storage.storage;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.reflect.annotation.AnnotationParser;

import java.io.IOException;
import java.util.*;

/**
 * TODO framework完善后移除
 */
public abstract class JacksonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();
    private static final Logger LOGGER = LoggerFactory.getLogger(JacksonUtils.class);

    static {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static <T> String toJSON(T t) {
        String json = "";
        try {
            json = mapper.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            LOGGER.error("An unexpected error occurred.", e);
        }
        return json;
    }

    public static <T> T toObject(String json, Class<T> clazz) {
        if (Strings.isNullOrEmpty(json)) {
            return null;
        }
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static Object toObject(String json, String name) {
        if (Strings.isNullOrEmpty(json)) {
            return null;
        }
        try {
            return mapper.readValue(json, Class.forName(name));
        } catch (IOException | ClassNotFoundException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static Object toObject(Map<String, Object> data, String name) {
        try {
            return mapper.convertValue(data, Class.forName(name));
        } catch (IllegalArgumentException | ClassNotFoundException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static <T> T toObject(Map<String, Object> data, Class<T> clz) {
        try {
            return mapper.convertValue(data, clz);
        } catch (IllegalArgumentException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static <T, V> T toParametricObject(String json, Class<T> parametrized, Class<V> clazz) {
        if (Strings.isNullOrEmpty(json)) {
            return null;
        }
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructParametricType(parametrized, clazz));
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static <T> T toParametricObject(String json, TypeReference valueTypeRef) {
        if (Strings.isNullOrEmpty(json)) {
            return null;
        }
        try {
            return mapper.readValue(json, valueTypeRef);
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static <T> List<T> toList(String json, Class<T> clazz) {
        if (Strings.isNullOrEmpty(json)) {
            return new ArrayList<>();
        }
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return new ArrayList<>();
        }
    }

    public static <T> Set<T> toSet(String json, Class<T> clazz) {
        if (Strings.isNullOrEmpty(json)) {
            return new HashSet<>();
        }
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(HashSet.class, clazz));
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return new HashSet<>();
        }
    }

    public static <K, V> Map<K, V> toMap(String json, Class<K> clazzK, Class<V> clazzV) {
        if (Strings.isNullOrEmpty(json)) {
            return new HashMap<>();
        }
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructMapType(HashMap.class, clazzK, clazzV));
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return new HashMap<>();
        }
    }

    public static Map<String, Object> toMap(String json) {
        if (Strings.isNullOrEmpty(json)) {
            return new HashMap<>();
        }
        try {
            return mapper.readValue(json, new TypeReference<Map<String, Object>>() {
            });
        } catch (IOException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return new HashMap<>();
        }
    }

    public static Map<String, Object> toMap(Object object) {
        try {
            return mapper.convertValue(object, new TypeReference<Map<String, Object>>() {
            });
        } catch (IllegalArgumentException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return new HashMap<>();
        }
    }

    public static Map<String, String> toStringMap(Object object) {
        try {
            return mapper.convertValue(object, new TypeReference<Map<String, String>>() {
            });
        } catch (IllegalArgumentException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return new HashMap<>();
        }
    }

    public static <T> T toObject(Object object, Class<T> clz) {
        try {
            return mapper.convertValue(object, clz);
        } catch (IllegalArgumentException e) {
            LOGGER.error("An unexpected error occurred.", e);
            return null;
        }
    }

    public static <T> String toJSON(T t, JsonView jsonView) {
        if (jsonView == null) {
            return toJSON(t);
        }
        ObjectWriter writer = mapper.writerWithView(jsonView.value()[0]);
        String json = "";
        try {
            json = writer.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            LOGGER.error("An unexpected error occurred.", e);
        }
        return json;
    }

    public static <T> String toJSON(T t, Class<?> viewClass) {
        return JacksonUtils.toJSON(t,
                (JsonView) AnnotationParser.annotationForMap(JsonView.class,
                        Collections.singletonMap("value", new Class<?>[]{viewClass})));
    }
}
